﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using RED_NET_Core.DAL;

namespace RED_NET_Core.BLL
{
    public class Manager_BLL
    {

        #region[构造函数]

        public Manager_BLL()
        { }

        #endregion

        Manager_DAL managerDAL = new Manager_DAL();
        Login_DAL loginDAL = new Login_DAL();

        #region[将Excel表导成DataSet]

        public DataSet GetDataSetByExcel(string fileNamePath, string table)
        {
            try
            {
                return managerDAL.TransformFromExcelToDataSet(fileNamePath, table);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        #endregion

        #region[利用dataset批量导入用户]

        public int BatchInsetIntoUser(DataSet ds)
        {
            try
            {
                return managerDAL.InsetIntoUser(ds);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        #endregion

        #region[单个注册用户]

        public bool Registe(string tUserName, string tPassword, int tCollegeId, int tMajorId, int tClassId,
            int tRight, string tNickName)
        {
            try
            {
                managerDAL.Registe(tUserName, tPassword, tCollegeId, tMajorId, tClassId, tRight, tNickName);
                if (tRight != 0)//如果注册的不是团支书，为其配置动态图
                {
                    int uid = loginDAL.IsValidUser(tUserName, tPassword);
                    string imageUrl = "../Image_Css/Image/Photo_Images/ttKoala3.jpg";//默认图片可到时候改
                    try
                    {
                        managerDAL.InsertTrendImage(uid, imageUrl);
                    }
                    catch (Exception ex)
                    {
                        throw ex;
                    }
                }
                return true;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        #endregion

        #region[修改密码]

        public bool UpdateMyData(string newPassword)
        {
            try
            {
                return managerDAL.UpdateMyPassword(newPassword);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        #endregion

        #region[修改头像]

        public bool UpdateMyAvatar(string avatarUrl)
        {
            try
            {
                return managerDAL.UpdateMyAvatar(avatarUrl);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        #endregion

        #region[返回密码]

        public string GetPassword(int tUserId)
        {
            try
            {
                return managerDAL.GetPassword(tUserId);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        #endregion

        #region[查看用户是否已经存在]

        public bool IsUserNameExist(string tUserName)
        {
            try
            {
                if (managerDAL.IsUserNameExist(tUserName) == 1) return true;
                else return false;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        #endregion

        #region[返回所有学院]

        public DataTable GetColleges()
        {
            try
            {
                return managerDAL.GetColleges();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        #endregion

        #region[修改对应用户的动态图对应图片地址]
        /// <summary>
        /// 修改动态图
        /// </summary>
        /// <param name="tUid">对应管理员Id</param>
        /// <param name="tPid">第几张图片</param>
        /// <param name="tNewUrl">新图片地址</param>
        /// <returns></returns>
        public bool UpdateTrendImage(int tUid, int tPid, string tNewUrl, int tNewsId)
        {
            try
            {
                return managerDAL.UpdateTrendImage(tUid, tPid, tNewUrl, tNewsId);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion

        #region[对应管理员的四张动态图地址]
        /// <summary>
        /// 获取对应管理员的四张动态图地址
        /// </summary>
        /// <param name="tUid">管理员ID</param>
        /// <returns>返回存储图片的数组ImageUrl[4]</returns>
        public string[] GetTrendOfManager(int tUid)
        {
            try
            {
                return managerDAL.GetTrendOfManager(tUid);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion

        #region[对应管理员的四张动态图链接的日志ID]
        /// <summary>
        /// 获取对应管理员的四张动态图地址
        /// </summary>
        /// <param name="tUid">管理员ID</param>
        /// <returns>返回存储图片的数组ImageUrl[4]</returns>
        public string[] GetNewsIdOfTrendImage(int tUid)
        {
            try
            {
                return managerDAL.GetNewsIdOfTrendImage(tUid);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion

        #region[对应管理员的四张动态图链接的日志名字]
        /// <summary>
        /// 获取对应管理员的四张动态图对应的日志名
        /// </summary>
        /// <param name="tUid">管理员ID</param>
        /// <returns>返回存储图片的数组ImageUrl[4]</returns>
        public string[] GetNewsNameOfTrendImage(int tUid)
        {
            try
            {
                return managerDAL.GetNewsNameOfTrendImage(tUid);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion

        #region[新注册的管理员插入四张图片]
        /// <summary>
        /// 新注册的管理员需要为其插入四张图片
        /// </summary>
        /// <param name="tUid">管理员id</param>
        /// <param name="tImageUrl">默认图片地址</param>
        /// <returns></returns>
        public bool InsertTrendImage(int tUid, String tImageUrl)
        {
            try
            {
                return managerDAL.InsertTrendImage(tUid, tImageUrl);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion

        //学院、学院用户增删改查（超级管理员）

        #region[新建学院、并插入学院管理员]

        //用户的_NickName和_CollegeName是等价的
        public bool InserNewCollege(string _NickName, string _UserName, string _Password)
        {
            int tCollegeId = 0;
            //如果该用户名不存在
            if (managerDAL.IsUserNameExist(_UserName) == 0)
            {
                try
                {
                    tCollegeId = managerDAL.InsertNewCollege(_NickName);//先新建学院
                }
                catch (Exception ex)
                {
                    throw ex;
                }
                try
                {
                    managerDAL.Registe(_UserName, _Password, tCollegeId, 0, 0, 1, _NickName);//新建学院管理员
                    return true;
                }
                catch (Exception ex)
                {
                    //如果新建学院管理员出错，那么删除学院表中的对应信息
                    try
                    {
                        managerDAL.DeleteCollege(tCollegeId);
                    }
                    catch (Exception ex1)
                    {
                        throw ex1;
                    }
                    throw ex;
                }
            }
            else//用户名已存在，注册失败
            {
                return false;
            }
        }

        #endregion

        #region[更改学院名、更改学院管理员昵称]

        public bool UpdateCollegeName(int _UId,string _Password,string _NickName, int _College)
        {
            try
            {
                //更新学院名
                managerDAL.UpdateCollegeName(_NickName, _College);
                //更新学院用户资料    ！——如果学院名更新成功了，这里出错，不知道怎么把学院名改回来，下一个任务。——！
                managerDAL.UpdateUser(_UId, _Password, _NickName);
                return true;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        #endregion

        #region[删除学院、学院管理员]

        //要两个参数的原因是删除两张表，参数来源全部有学院管理员的表提供
        public bool DeleteCollege(int _UId,int _College)
        {
            try
            {
                managerDAL.DeleteCollege(_College);
                managerDAL.DeleteUser(_UId);
                return true;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        #endregion

        #region[查找学院用户]

        public DataTable SelectCollegeUsers()
        {
            try
            {
                return managerDAL.SelectCollegeUsers();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        #endregion

        //对应学院专业的增删改查（超级管理员）

        #region[新建专业]

        public bool InsertNewMajor(string _MajorName, int _College)
        {
            try
            {
                return managerDAL.InserNewMajor(_MajorName, _College);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        #endregion

        #region[更新专业]

        public bool UpdateMajor(int _MajorId, string _MajorName)
        {
            try
            {
                return managerDAL.UpdateMajor(_MajorId, _MajorName);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        #endregion

        #region[删除专业]

        public bool DeleteMajor(int _MajorId)
        {
            try
            {
                return managerDAL.DeleteMajor(_MajorId);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        #endregion

        #region[查找专业]

        public DataTable GetMajorsByCollege(int _College)
        {
            try
            {
                return managerDAL.GetMajorsByCollege(_College);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        #endregion

        //班级的增删改查（管理员）

        #region[新建班级]

        public int InserNewClass(string _ClassName, int _MajorId, int _CollegeId)
        {
            try
            {
                return managerDAL.InserNewClass(_ClassName, _MajorId, _CollegeId);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        #endregion

        #region[更新班级]

        public bool UpdateClass(int _ClassId, string _ClassName)
        {
            try
            {
                return managerDAL.UpdateClass(_ClassId, _ClassName);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        #endregion

        #region[删除班级]

        public bool DeleteClass(int _ClassId)
        {
            try
            {
                return managerDAL.DeleteClass(_ClassId);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        #endregion

        #region[查找班级(学院查找）]

        public DataTable GetClassesByCollege(int _CollegeId)
        {
            try
            {
                return managerDAL.GetClassesByCollege(_CollegeId);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        #endregion

        #region[查找班级(专业查找）]

        public DataTable GetClassesByMajor(int _MajorId)
        {
            try
            {
                return GetClassesByMajor(_MajorId);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        #endregion

        //团支书用户的增删改查（管理员）

        #region[添加团支书用户]

        public bool InsertNewClass(string nickName, string userName, string password, int collegeId, int majorId)
        {
            if (managerDAL.IsUserNameExist(userName) == 0)
            {
                try
                {
                    int classId = managerDAL.InserNewClass(nickName, majorId, collegeId);
                    int classUserId = managerDAL.Registe(userName, password, collegeId, majorId, classId, 0, nickName);
                    managerDAL.Update_InserNewClass_ClassUserId(classUserId);
                    return true;
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }
            else//用户名已存在
            {
                return false;
            }
        }

        #endregion

        #region[删除团支书用户]

        public bool DeleteClassUser(int UId, int ClassId)
        {
            try
            {
                managerDAL.DeleteClass(ClassId);
                managerDAL.DeleteUser(UId);
                return true;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        #endregion

        #region[修改团支书用户]

        public bool UpdateClassUser(int UId, string password, string nickName, int classId)
        {
            try
            {
                managerDAL.UpdateClass(classId, nickName);
                managerDAL.UpdateUser(UId, password, nickName);
                return true;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        #endregion

        #region[查找团支书用户(根据学院）]

        public DataTable SelectClassUsersByCollege(int tCollegeId)
        {
            try
            {
               return managerDAL.SelectClassUsersByCollege(tCollegeId);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        #endregion

        #region[查找团支书用户(根据专业)]

        public DataTable SelectClassUsersByMajor(int tMajorId)
        {
            try
            {
                return managerDAL.SelectClassUsersByMajor(tMajorId);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        #endregion
    }
}
